package com.hy.convenient.tool.xlsx.interfaces;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 初始化验证类
 *
 * @author hdy
 * @date 2019年09月06日 11:13
 */
@Component
public class CheckTypeContext implements ApplicationContextAware {
    private static Map<CheckTypeEnum, Object> checkTypeMap = new ConcurrentHashMap<>();
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        String[] clazz = applicationContext.getBeanNamesForAnnotation(CheckTypeAnnotation.class);
        for (String str : clazz) {
            Object bean = applicationContext.getBean(str);
            CheckTypeAnnotation annotation = bean.getClass().getAnnotation(CheckTypeAnnotation.class);
            if(annotation!=null){
                checkTypeMap.put(annotation.value(), bean);
            }
        }
    }

    public static Object getCheckTypeMap(CheckTypeEnum checkTypeEnum) {
        return checkTypeMap.getOrDefault(checkTypeEnum, null);
    }
}
